package com.zmx;

import com.zmx.job.admin.entity.MallJobInfo;
import com.zmx.job.admin.repository.MallJobInfoRepository;
import com.zmx.job.admin.service.MallJobInfoService;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

/**
 * @Description: 设置任务
 * @ClassName: SetMallJobInfo
 * @Author zhaomxr
 * @Date 2021-12-22 14:00
 */
@RunWith(SpringRunner.class)
@SpringBootTest
public class SetMallJobInfo {

    @Autowired
    private MallJobInfoService mallJobInfoService;
    @Autowired
    private MallJobInfoRepository mallJobInfoRepository;

    /**
     * 添加任务
     */
    @Test
    public void addJob() {
        MallJobInfo mallJobInfo = new MallJobInfo();
        mallJobInfo.setAuthor("zhaomx");
        mallJobInfo.setAlarmEmail("zhaomx1995@126.com");
        //任务名称
        mallJobInfo.setExecutorHandler("testJob");
        //分发任务路由策略 随机分发、轮询分发、最佳性能分发 三种
        mallJobInfo.setExecutorRouteStrategy("random");
        //任务超时时间
        mallJobInfo.setExecutorTimeout(5000);
        mallJobInfo.setJobDesc("测试任务");
        //任务执行时间cron 当前是每隔一分钟执行一次
        mallJobInfo.setScheduleConf("0 */1 * * * ?");
        mallJobInfoService.save(mallJobInfo);
    }

    /**
     * 更新任务
     */
    @Test
    public void updateJob() {
        String name = "";
        MallJobInfo mallJobInfo = mallJobInfoRepository.findByExecutorHandler(name);

        //分发任务路由策略 随机分发、轮询分发、最佳性能分发 三种
        mallJobInfo.setExecutorRouteStrategy("random");
        //任务超时时间
        mallJobInfo.setExecutorTimeout(5000);
        //任务执行时间cron
        mallJobInfo.setScheduleConf("0 */1 * * * ?");
        mallJobInfoService.update(mallJobInfo);
    }

    @Test
    public void stopJob() {
        String name = "";
        MallJobInfo mallJobInfo = mallJobInfoRepository.findByExecutorHandler(name);
        mallJobInfoService.stop(mallJobInfo.getId());
    }

    @Test
    public void startJob() {
        String name = "";
        MallJobInfo mallJobInfo = mallJobInfoRepository.findByExecutorHandler(name);
        mallJobInfoService.start(mallJobInfo.getId());
    }
}
